package com.china08.yxyapi.api.home.webgroup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.sws.EdbWebGroupService;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.ThumborUtil;
import com.squareup.pollexor.ThumborUrlBuilder;
import com.squareup.pollexor.ThumborUrlBuilder.ImageFormat;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class WebGroupListController extends YxyApiBaseController {

    @Autowired
    EdbWebGroupService edbWebGroupService;

    @ApiOperation("")
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/api_yw/home/webgroup/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String provinceId = reqModel.provinceId;
        String cityId = reqModel.cityId;
        String districtId = reqModel.districtId;
        String phase = reqModel.phase;
        String paixu = reqModel.paixu;
        String keyword = StringUtils.isNotBlank(reqModel.keyword) ? "%" + reqModel.keyword + "%" : "";
        Integer pageNum = reqModel.pageNum;

        Integer allPage = 0;
        List<Pojo> retrunList = new ArrayList<Pojo>();

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> map = edbWebGroupService.HomeSchoolGroupList(provinceId, cityId, districtId, phase, paixu, keyword, pageNum);
        if (map != null) {
            allPage = Integer.parseInt(map.get("allPage") + "");
            list = (List<Map<String, Object>>) map.get("list");

            for (Map<String, Object> m : list) {
                Pojo pojo = new Pojo();
                pojo.schoolId = m.get("school_id") + "";
                pojo.schoolName = m.get("school_name") + "";

                pojo.address = m.get("address") + "";
                pojo.webUrl = m.get("web_url") + "";

                boolean bb0 = (boolean) m.get("is_st0");
                boolean bb1 = (boolean) m.get("is_st1");
                boolean bb2 = (boolean) m.get("is_st2");
                boolean bb3 = (boolean) m.get("is_st3");

                List<String> schoolTypeList = new ArrayList<>();
                if (bb0) {
                    schoolTypeList.add("幼儿园");
                }
                if (bb1) {
                    schoolTypeList.add("小学");
                }
                if (bb2) {
                    schoolTypeList.add("初中");
                }
                if (bb3) {
                    schoolTypeList.add("高中");
                }
                String schoolBadge = m.get("school_badge") + "";
                if (StringUtils.isBlank(schoolBadge) || "null".equals(schoolBadge)) {
                    schoolBadge = "https://yxyosc.91yunxiao.com/yxyosc/2017/10/16/ca1ab5a0786881b2a231554bb3890925.png";
                }
                if (!schoolBadge.startsWith("http")) {
                    schoolBadge = "https://yxyosc.91yunxiao.com/yxyosc/" + schoolBadge;
                }

                try {
                    // 默认图片不做图片截取处理
                    boolean b1 = schoolBadge.equals("https://yxyosc.91yunxiao.com/yxyosc/2017/10/16/ca1ab5a0786881b2a231554bb3890925.png");

                    // 是否是gif图片
                    boolean b2 = schoolBadge.endsWith("gif") || schoolBadge.endsWith("GIF");

                    //
                    if (!b1 && !b2) {
                        ThumborUrlBuilder tub = ThumborUtil.getThumborInstance().buildImage(schoolBadge);
                        tub.resize(60, 60);
                        tub.smart();
                        tub.trim();
                        tub.filter(ThumborUrlBuilder.quality(100));
                        if (schoolBadge.endsWith("jpg") || schoolBadge.endsWith("jpeg") || schoolBadge.endsWith("JPG") || schoolBadge.endsWith("JPEG")) {
                            tub.filter(ThumborUrlBuilder.format(ImageFormat.JPEG));
                        } else if (schoolBadge.endsWith("PNG") || schoolBadge.endsWith("png")) {
                            tub.filter(ThumborUrlBuilder.format(ImageFormat.PNG));
                        } else if (schoolBadge.endsWith("bmp") || schoolBadge.endsWith("BMP")) {
                            tub.filter(ThumborUrlBuilder.format(ImageFormat.PNG));
                        } else if (schoolBadge.endsWith("gif") || schoolBadge.endsWith("GIF")) {
                            tub.filter(ThumborUrlBuilder.format(ImageFormat.GIF));
                        }
                        schoolBadge = tub.toUrl();
                    }
                } catch (Exception e) {
                }
                pojo.schoolBadge = schoolBadge;
                pojo.schoolType = StringUtils.join(schoolTypeList.toArray(), ",");
                pojo.pv = m.get("pv") + "";
                pojo.articleCount = m.get("article_count") + "";
                pojo.latestArticle = DateToolUtils.format((Date) m.get("latest_article"), "yyyy-MM-dd");
                retrunList.add(pojo);
            }
        }

        RespModel respModel = new RespModel();
        respModel.allPage = allPage;
        respModel.list = retrunList;
        return respModel;
    }

    public static class Pojo {
        public String schoolId;
        public String schoolName;
        public String schoolBadge;
        public String address;
        public String areaNameProvince;
        public String areaNameCity;
        public String areaNameDistrict;
        public String isSt0;
        public String isSt1;
        public String isSt2;
        public String isSt3;
        public String schoolType;
        public String latestArticle;
        public String pv;
        public String articleCount;
        public String webUrl;
    }

    public static class ReqModel extends YxyApiPageReqModel {
        public String provinceId;
        public String cityId;
        public String districtId;
        public String phase;
        public String paixu;
        public String keyword;
        public Integer pageNum;

        @Override
        public void autoCheck() {
            Assert.isTrue((StringUtils.isNotBlank(provinceId)) || (StringUtils.isNotBlank(cityId)) || (StringUtils.isNotBlank(districtId)), "未获取到教育局地址");
        }

    }

    public static class RespModel extends YxyApiRespModel {
        public Integer allPage;
        public List<Pojo> list;
    }
}
